﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Media3D;
using System.Xml;

using KELT;

namespace KELT.KELTObjects.ViewportObjectModel
{
    class ParticleObject : ViewportObj
    {
        private TextBlock frontText;
        private TextBlock backText;

        private FontFamily fontFamily;

        public ParticleObject(TOOL mainContext, double x, double y, String text)
        {
            ObjectInfo = "ParticleObject";
            double changeX = (x * 2 / mainContext.mainViewport.RenderSize.Width - 1) * (0.4 * mainContext.cameraManager.getPoint3D().Z) + mainContext.cameraManager.getPoint3D().X + mainContext.cameraManager.getTranslateTransform().OffsetX;
            double changeY = (y * -2 / mainContext.mainViewport.RenderSize.Height + 1) * (0.24 * mainContext.cameraManager.getPoint3D().Z) + mainContext.cameraManager.getPoint3D().Y + mainContext.cameraManager.getTranslateTransform().OffsetY;

            width = text.Length;
            height = text.Length;

            indexNumber = mainContext.viewPortMgr.INDEXNUMBER;
            objectName = ObjectInfo + indexNumber;

            fontFamily = new FontFamily();
            point = new Point3D(changeX, changeY, 0);

            frontViewBox = new Viewbox();
            frontViewBox.Name = "object" + indexNumber;

            FrontViusal = new Viewport2DVisual3D();
            colorBrush = new SolidColorBrush();

            CreateTextBlock(text);

            frontViewBox.Child = frontText;
            FrontViusal = CreateFront(frontViewBox, FrontTransform3DGroup);


            CreateBack(mainContext);

            backText = new TextBlock();
            backText.Text = text;

            backText.Foreground = colorBrush;
            backText.FontFamily = fontFamily;


            backText.FontSize = frontText.FontSize;
            BackViewBox.Child = backText;


            frontViewBox.MouseDown += new MouseButtonEventHandler(mainContext.Object_MouseDown);
            frontViewBox.MouseMove += new MouseEventHandler(mainContext.Object_MouseMove);
            frontViewBox.MouseRightButtonDown += new MouseButtonEventHandler(mainContext.EncoderWindow_MouseRightButtonDown);


            isVisual = true;
            mainContext.mainViewport.Children.Add(FrontViusal);


            mainContext.RegisterName(AnimationInfo.ANIMATION_ALPHA + indexNumber, colorBrush);
            mainContext.RegisterName(AnimationInfo.ANIMATION_TRANSLATE + indexNumber, FrontTransform3DGroup.Children.ElementAt(0));
            mainContext.RegisterName(AnimationInfo.ANIMATION_SCALE + indexNumber, FrontTransform3DGroup.Children.ElementAt(1));
            mainContext.RegisterName(AnimationInfo.ANIMATION_ROTATE + indexNumber, FrontTransform3DGroup.Children.ElementAt(2));

        }

        private void CreateTextBlock(String text)
        {
            frontText = new TextBlock();
            frontText.Text = text;

            frontText.Foreground = colorBrush;

            FontSizeConverter fSizeConverter = new FontSizeConverter();
            frontText.FontSize = (double)fSizeConverter.ConvertFromString("5pt");
        }


        public void setTextEffect(TOOL mainContext)
        {
            frontText.TextEffects = new TextEffectCollection();
            frontText.Name = objectName;
            for (int i = 0; i < frontText.Text.Length; ++i)
            {
                AddTextEffectForCharacter(i);
            }
            mainContext.RegisterName("TESTPARTICLE" + indexNumber, frontText);
        }

        public void AddTextEffectForCharacter(int charIndex)
        {
            TextEffect effect = new TextEffect();

            effect.PositionStart = charIndex;
            effect.PositionCount = 1;

            TransformGroup transfGrp = new TransformGroup();

            TranslateTransform translate = new TranslateTransform();

            transfGrp.Children.Add(translate);


            RotateTransform rtotate = new RotateTransform();
            rtotate.CenterX = 7;
            rtotate.CenterY = 5;

            transfGrp.Children.Add(rtotate);
            effect.Transform = transfGrp;

            frontText.TextEffects.Add(effect);
        }



        public void setFontFamily(string str)
        {
            fontFamily = new FontFamily(str);
            frontText.FontFamily = fontFamily;
            backText.FontFamily = fontFamily;
        }

        public void setColor(Color color)
        {
            colorBrush.Color = color;
        }

        public TextBlock getText()
        {
            return frontText;
        }

        #region XML
        public override void GetObjInfoXML(XmlWriter writer)
        {
            writer.WriteStartElement("ParticleObject");
            {
                //writer.WriteElementString("fileSource", );
            }
            writer.WriteEndElement();
        }
        #endregion


    }
}
